package com.mlizhi.modules.spec.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.mlizhi.kingdom.R;

import android.annotation.SuppressLint;
import android.content.Context;

@SuppressLint("SimpleDateFormat")
public class DateFormatUtil {
	public static Calendar getBeginDateByWeek(Date date, int weekTh) {
		int off = 0;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int dayofweek = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (dayofweek <= 0) {
			dayofweek = 7; // 周日是7
		}

		off = dayofweek - 1;
		int off1 = weekTh * 7 + off;
		// int off2 = off1 - 7;
		cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - off1);// 让日期加1

		return cal;
	}

	public static Calendar getBeginDateByMonth(Date date, int monthTh) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date); // 设置为当前日期

		int year_off = monthTh / 12; // 年份偏移量
		int month_off = monthTh % 12; // 月份偏移量

		cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) - year_off);
		cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - month_off);
		cal.set(Calendar.DAY_OF_MONTH, 1);

		return cal;
	}

	public static String getDatethInWeek(Date date, Context context) {
		String[] weeks = { context.getString(R.string.week4sun),
				context.getString(R.string.week4mon),
				context.getString(R.string.week4tue),
				context.getString(R.string.week4wed),
				context.getString(R.string.week4thu),
				context.getString(R.string.week4fri),
				context.getString(R.string.week4sat) };

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (week_index < 0) {
			week_index = 0;
		}
		return weeks[week_index];
	}

	/**
	 * 获取当前时间是一年中的第几周
	 * @param currentDate
	 * @param context
	 * @return
	 */
	public static int getWeekthInYear(Date currentDate) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(currentDate);
		int weekth = cal.get(Calendar.WEEK_OF_YEAR);

		getBeginDateByWeek(currentDate, weekth);
		return weekth;
	}


	/**
	 * 取得指定日期所在周的第一天
	 */
	public static Date getFirstDayOfWeek(Date dNow) {
//		Date dNow = str2date(currentDate, context); // 当前时间
		Calendar c = Calendar.getInstance();
//		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(dNow);
		c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // Monday
//		c.add(Calendar.WEEK_OF_YEAR, 1);
		return c.getTime();
	}

	/**
	 * 取得指定日期所在周的最后一天
	 */
	public static Date getLastDayOfWeek(Date dNow) {
//		Date dNow = str2date(currentDate, context); // 当前时间
		Calendar c = Calendar.getInstance();
		c.setTime(dNow);
		 //这种输出的是上个星期周日的日期，因为老外那边把周日当成第一天
		c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); // Sunday
		 //增加一个星期，才是我们中国人理解的本周日的日期
		c.add(Calendar.WEEK_OF_YEAR, 1);
		return c.getTime();
	}
	
	

	/**
	 * 获取当前日期的前一天
	 * @param currentDate
	 * @param context
	 * @return
	 */
	public static Date getBeforeDay(Date currentDate) {
		Date dBefore = new Date();
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(currentDate);// 把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
		dBefore = calendar.getTime(); // 得到前一天的时间

		return dBefore;
	}

	/**
	 * 获取当前日期的后一天
	 * @param currentDate
	 * @param context
	 * @return
	 */
	public static Date getNextDay(Date currentDate) {
		Date dAfter = new Date();

		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(currentDate);// 把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, 1); // 设置为前一天
		dAfter = calendar.getTime(); // 得到前一天的时间
		return dAfter;
	}
	
	
	/**
	 * 获取当前日期的前一天
	 * @param currentDate
	 * @param context
	 * @return
	 */
	@Deprecated
	public static Date getBeforeDay(String currentDate) {
		Date dBefore = new Date();
		Date dNow = str2date(currentDate);
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(dNow);// 把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
		dBefore = calendar.getTime(); // 得到前一天的时间

		return dBefore;
	}

	/**
	 * 获取当前日期的后一天
	 * @param currentDate
	 * @param context
	 * @return
	 */
	@Deprecated
	public static Date getNextDay(String currentDate) {
		Date dNow = str2date(currentDate); // 当前时间
		Date dAfter = new Date();

		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(dNow);// 把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, 1); // 设置为前一天
		dAfter = calendar.getTime(); // 得到前一天的时间
		return dAfter;
	}

	public static Date formatDate(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");// 定义日期格式
		format.setLenient(false); // 执行严格检测
		Date fomatDate = null;
		String tempDate = "";
		try {
			tempDate = format.format(date);
			fomatDate = format.parse(tempDate);
			// 将字符串转换为日期
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return fomatDate;
	}
	
	
	public static Date str2date(String dateStr) {
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");// 定义日期格式
		format.setLenient(false); // 执行严格检测
		Date date = null;
		try {
			date = format.parse(dateStr);// 将字符串转换为日期
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String date2str(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
		String dateStr = sdf.format(date); // 格式化前一天
		return dateStr;
	}

	
	public static String date2string(Date date, Context context) {
		StringBuffer sb = new StringBuffer();
		// sb.append("yyyy");
		// sb.append(context.getString(R.string.date4year));
		sb.append("MM");
		sb.append(context.getString(R.string.date4month));
		sb.append("dd");
		sb.append(context.getString(R.string.date4date));
		SimpleDateFormat sdf = new SimpleDateFormat(sb.toString()); // 设置时间格式
		String dateStr = sdf.format(date); // 格式化前一天
		return dateStr;
	}
	@Deprecated
	public static Date getBeforeWeekCurrentDate(String currentDate) {
		Date dNow = str2date(currentDate); // 当前时间
		Date dAfter = new Date();
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(dNow);// 把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, -7); // 设置为前7天
		dAfter = calendar.getTime(); // 得到前7天的时间
		return dAfter;
	}

	@Deprecated
	public static Date getNextWeekCurrentDate(String currentDate) {
		Date dNow = str2date(currentDate); // 当前时间
		Date dAfter = new Date();
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(dNow);// 把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, 7); // 设置为前7天
		dAfter = calendar.getTime(); // 得到前7天的当前时间
		return dAfter;
	}
	
	public static Date getBeforeWeekCurrentDate(Date currentDate) {
		Date dAfter = new Date();
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(currentDate);// 把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, -7); // 设置为前7天
		dAfter = calendar.getTime(); // 得到前7天的时间
		return dAfter;
	}

	public static Date getNextWeekCurrentDate(Date currentDate) {
		Date dAfter = new Date();
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(currentDate);// 把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, 7); // 设置为前7天
		dAfter = calendar.getTime(); // 得到前7天的当前时间
		return dAfter;
	}

	@Deprecated
	public static Date getBeforeMonthCurrentDate(String currentDate) {
		Date dNow = str2date(currentDate); // 当前时间
		Date dAfter = new Date();
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(dNow);// 把当前时间赋给日历
		calendar.add(Calendar.MONTH, -1); // 设置为前一月
		dAfter = calendar.getTime(); // 得到前一月的当前时间
		return dAfter;
	}

	@Deprecated
	public static Date getNextMonthCurrentDate(String currentDate) {
		Date dNow = str2date(currentDate); // 当前时间
		Date dAfter = new Date();
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(dNow);// 把当前时间赋给日历
		calendar.add(Calendar.MONTH, 1); // 设置为前一月
		dAfter = calendar.getTime(); // 得到前一月的当前时间
		return dAfter;
	}
	
	public static Date getBeforeMonthCurrentDate(Date currentDate) {
		Date dAfter = new Date();
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(currentDate);// 把当前时间赋给日历
		calendar.add(Calendar.MONTH, -1); // 设置为前一月
		dAfter = calendar.getTime(); // 得到前一月的当前时间
		return dAfter;
	}

	public static Date getNextMonthCurrentDate(Date currentDate) {
		Date dAfter = new Date();
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(currentDate);// 把当前时间赋给日历
		calendar.add(Calendar.MONTH, 1); // 设置为前一月
		dAfter = calendar.getTime(); // 得到前一月的当前时间
		return dAfter;
	}

	/**
	 * 取得当前日期是多少周
	 * 
	 * @param date
	 * @return
	 */
	public static int getWeekOfYear(Date date) {
		Calendar c = Calendar.getInstance();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		/**
		 * 设置一年中第一个星期所需的最少天数，例如，如果定义第一个星期包含一年第一个月的第一天，则使用值 1 调用此方法。
		 * 如果最少天数必须是一整个星期，则使用值 7 调用此方法。
		 **/
		c.setMinimalDaysInFirstWeek(1);
		c.setTime(date);

		return c.get(Calendar.WEEK_OF_YEAR);
	}

	/**
	 * 得到某一年周的总数
	 * 
	 * @param year
	 * @return
	 */
	public static int getMaxWeekNumOfYear(int year) {
		Calendar c = Calendar.getInstance();
		c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);

		return getWeekOfYear(c.getTime());
	}

	/**
	 * 得到某年某周的第一天
	 * 
	 * @param year
	 * @param week
	 * @return
	 */
	public static Date getFirstDayOfWeek(int year, int week) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.WEEK_OF_YEAR, week);
		c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// 设置周一
		c.setFirstDayOfWeek(Calendar.MONDAY);

		return c.getTime();
	}

	/**
	 * 得到某年某周的最后一天
	 * 
	 * @param year
	 * @param week
	 * @return
	 */
	public static Date getLastDayOfWeek(int year, int week) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.WEEK_OF_YEAR, week);
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday

		return c.getTime();
	}

	/**
	 * 得到某年某月的第一天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date getFirstDayOfMonth(int year, int month) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));

		return c.getTime();
	}

	/**
	 * 得到某年某月的最后一天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date getLastDayOfMonth(int year, int month) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));

		return c.getTime();
	}

	
	
	/**
	 * 得到某年某月的第一天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date getFirstDayOfMonth(Date dNow) {
		Calendar c = Calendar.getInstance();
		c.setTime(dNow);
		c.set(Calendar.YEAR, c.get(Calendar.YEAR));
		c.set(Calendar.MONTH,  c.get(Calendar.MONTH));
		c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));

		return c.getTime();
	}

	/**
	 * 得到某年某月的最后一天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date getLastDayOfMonth(Date dNow) {
		Calendar c = Calendar.getInstance();
		c.setTime(dNow);
		c.set(Calendar.YEAR, c.get(Calendar.YEAR));
		c.set(Calendar.MONTH,  c.get(Calendar.MONTH));
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		return c.getTime();
	}

	
	/**
	 * 得到某年某季度第一天
	 * 
	 * @param year
	 * @param quarter
	 * @return
	 */
	public static Date getFirstDayOfQuarter(int year, int quarter) {
		int month = 0;
		if (quarter > 4) {
			return null;
		} else {
			month = (quarter - 1) * 3 + 1;
		}
		return getFirstDayOfMonth(year, month);
	}

	/**
	 * 得到某年某季度最后一天
	 * 
	 * @param year
	 * @param quarter
	 * @return
	 */
	public static Date getLastDayOfQuarter(int year, int quarter) {
		int month = 0;
		if (quarter > 4) {
			return null;
		} else {
			month = quarter * 3;
		}
		return getLastDayOfMonth(year, month);
	}

	/**
	 * 得到某年第一天
	 * 
	 * @param year
	 * @return
	 */
	public static Date getFirstDayOfYear(int year) {
		return getFirstDayOfQuarter(year, 1);
	}

	/**
	 * 得到某年最后一天
	 * 
	 * @param year
	 * @return
	 */
	public static Date getLastDayOfYear(int year) {
		return getLastDayOfQuarter(year, 4);
	}

}
